package cn.com.ksplatform.common.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;

import cn.com.ksplatform.common.logger.Log;
import cn.com.ksplatform.install.model.InstallInfo;
import cn.com.ksplatform.install.service.DBType;

/**
 * 研发平台系统工具类
 * 
 * SystemUtil
 * 
 * 2015年3月19日 上午9:20:45
 * @author Mr.Xiao
 * @version 1.0.0
 *
 */
public class SystemUtil {
	private static String path = System.getProperty("catalina.home");
	/**
	 * 持久化安装状态
	 * saveInstallStatu()
	 * (这里描述这个方法适用条件 – 可选) 
	 * @return void
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @exception 
	 * @since  1.0.0
	 */
	public static void saveInstallStatu(InstallInfo model,String contextPath) throws FileNotFoundException, IOException{
		File bf = new File(path+File.separator+contextPath+".boot");
		if(!bf.exists()){
			bf.createNewFile();
		}
		ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(bf));
		oos.writeObject(model);
		oos.flush();
		oos.close();
		Log.info("系统安装成功");
	}
	/**
	 * 加载系统配置Model
	 * getSysConfigModel()
	 * (这里描述这个方法适用条件 – 可选)
	 * @param contextPath
	 * @return 
	 * @return InstallInfo
	 * @throws IOException 
	 * @throws FileNotFoundException 
	 * @throws ClassNotFoundException 
	 * @exception 
	 * @since  1.0.0
	 */
	public static InstallInfo getSysConfigModel(String contextPath) throws FileNotFoundException, IOException, ClassNotFoundException{
		File bf = new File(path+File.separator+contextPath+".boot");
		if(!bf.exists()){
			return null;
		}
		ObjectInputStream ois = new ObjectInputStream(new FileInputStream(bf));
		Object config = ois.readObject();
		ois.close();
		return (InstallInfo) config;
	}
	/**
	 * 加载系统配置Model
	 * @return
	 * @throws FileNotFoundException
	 * @throws IOException
	 * @throws ClassNotFoundException
	 */
	public static InstallInfo getSysConfigModel() throws FileNotFoundException, IOException, ClassNotFoundException{
		JAXBContext context;
		InputStream ins = null;
		try {
			context = JAXBContext.newInstance(InstallInfo.class);
			Unmarshaller unmarshaller = context.createUnmarshaller();
			ins = SystemUtil.class.getClassLoader().getResourceAsStream("config.xml");
			if(ins!=null){
				return (InstallInfo) unmarshaller.unmarshal(ins);
			}
			
		} catch (JAXBException e) {
			e.printStackTrace();
		}finally{
			try {
				if(ins!=null){
					ins.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	/**
	 * 判断boot文件是否存在 
	 * existsBoot()
	 * (这里描述这个方法适用条件 – 可选)
	 * @param contextPath
	 * @return 
	 * @return boolean
	 * @exception 
	 * @since  1.0.0
	 */
	public static boolean existsBoot(String contextPath){
		InputStream ins = SystemUtil.class.getClassLoader().getResourceAsStream("config.xml");
		if(ins != null){
			return true;
		}
		File bf = new File(path+File.separator+contextPath+".boot");
		if(bf.exists()){
			return true;
		}
		
		return false;
	}
	
	public static void main(String[] args) throws FileNotFoundException, IOException {
		InstallInfo info = new InstallInfo();
		
		info.setDataBaseType(DBType.MySql.getValue());
		info.setDataBaseHost("192.168.53.17");
		info.setDataBaseName("emplatformfk");
		info.setDataBaseUserName("emp_dev");
		info.setDataBasePassWord("emp_dev");
		info.setDataBasePort(3306);
		
		String url = "jdbc:mysql://"+info.getDataBaseHost()+":"+info.getDataBasePort()+"/"+info.getDataBaseName()+"?characterEncoding=utf-8";
		info.setDataBaseUrl(url);
		
		info.hibernateProperties = "hibernate.dialect=org.hibernate.dialect.MySQL5Dialect";
		
	/*	ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(new File("d:\\emplatform.boot")));
		oos.writeObject(info);
		oos.flush();
		oos.close();*/
		OutputStream ous =new FileOutputStream(new File("d:\\api.xml"));
		JAXBContext context;
		try {
			context = JAXBContext.newInstance(info.getClass());
			Marshaller marshaller = context.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");// //编码格式
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);// 是否格式化生成的xml串
			marshaller.marshal(info, ous);
		} catch (JAXBException e) {
			e.printStackTrace();
		}finally{
			try {
				ous.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
